﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MemorySiteServer.AuthManage
{
    public class UserCode
    {
        public UserCode()
        {
            ClientID = Guid.NewGuid().ToString();
            BuildTime = DateTime.Now;
        }
        /// <summary>
        /// 客户端ID
        /// </summary>
        public string ClientID { get;private set; }
        /// <summary>
        /// 服务器端生成的验证码
        /// </summary>
        public string ServerCode { get; set; }
        /// <summary>
        /// 验证码建立刷新时间
        /// </summary>
        public DateTime BuildTime { get; set; }

        /// <summary>
        /// 刷新验证码，但是不会改变ClientID
        /// </summary>
        /// <returns></returns>
        public string CreateCode()
        {
            MemoryStream ms = VierificationCodeHelper.Create(out string code);
            byte[] imgByte = new byte[ms.Length];
            ms.Seek(0,SeekOrigin.Begin);
            ms.Read(imgByte, 0, imgByte.Length);
            ServerCode = code.ToUpperInvariant();
            return "data:image/jpeg;base64," + Convert.ToBase64String(imgByte);
        }
    }
}
